//
//  CJCustomLoadingAnimationView.m
//  DatangLife
//
//  Created by 陈建 on 2018/4/22.
//  Copyright © 2018年 DatangLife. All rights reserved.
//

#import "CJCustomLoadingAnimationView.h"

#define ImageName @"refreshjoke_loading_"
#define ImageCount 10

@interface CJCustomLoadingAnimationView ()
@property (nonatomic, weak) UIImageView *imageView;
@property (nonatomic, strong) NSMutableArray *imageArray;

@end



@implementation CJCustomLoadingAnimationView

- (instancetype)init{
    self = [super init];
    if (self) {
        self.backgroundColor = kClearColor;
    }
    return self;
}

- (void)showInView:(UIView *)view{
    if (view == nil) {
        view = [UIApplication sharedApplication].keyWindow;
    }
    [view addSubview:self];
    self.frame = view.bounds;
    self.imageView.frame = CGRectMake(0, 0, 57, 66);
    self.imageView.center = self.center;
    [self.imageView startAnimating];
}

- (void)dismiss{
    [_imageArray removeAllObjects];
    [_imageView stopAnimating];
    [_imageView removeFromSuperview];
    [self removeFromSuperview];
}

- (NSMutableArray *)imageArray{
    if (!_imageArray) {
        _imageArray = [NSMutableArray new];
    }
    return _imageArray;
}

- (UIImageView *)imageView{
    if (!_imageView) {
        UIImageView *imgV = [[UIImageView alloc] init];
        [self addSubview:imgV];
        _imageView = imgV;
        
        for (NSInteger i = 0; i<ImageCount; i ++) {
            NSString *imageN = [ImageName stringByAppendingString:[NSString stringWithFormat:@"%ld",i % ImageCount]];
            UIImage *image = [UIImage imageNamed:imageN];
            [self.imageArray addObject:image];
        }
        self.imageView.animationDuration = 0.7;
        self.imageView.animationRepeatCount = 0;
        self.imageView.animationImages = self.imageArray;
    }
    return _imageView;
}

@end
